package cn.pomit.springbootwork.lock.support.web;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import cn.pomit.springbootwork.lock.support.model.TGoodInfo;
import cn.pomit.springbootwork.lock.support.service.GoodsInfoService;

/**
 * 商品服务接口，供多个服务调用
 * 
 * @author fufei
 *
 */
@RestController
@RequestMapping("/goods")
public class GoodsInfoRest {
	private Logger log = LoggerFactory.getLogger(this.getClass());
	@Autowired
	GoodsInfoService goodsInfoService;

	@RequestMapping(value = "/take")
	public TGoodInfo take(@RequestParam("goodId") Integer goodId) {
		return goodsInfoService.takeOne(goodId);
	}

	@RequestMapping(value = "/page")
	public List<TGoodInfo> take(@RequestParam(value = "page", required = false) Integer page,
			@RequestParam(value = "size", required = false) Integer size) {
		if (page == null)
			page = 0;
		if (size == null)
			size = 10;
		return goodsInfoService.findPage(page, size).getContent();
	}

	@RequestMapping(value = "/consume")
	public int consume(@RequestParam("goodId") Integer goodId, @RequestParam("num") Integer num,
			@RequestParam("serverId") Integer serverId) {
		try {
			log.info("收到客户端消费请求：serverId：{};goodId:{}", serverId, goodId);
			return goodsInfoService.consume(goodId, num);
		} catch (Exception e) {
			log.info("处理异常：{};", e.getMessage());
			return -3;
		}
	}
}
